﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;

namespace WikiDoc.Lines
{
    public class DefineListLine : WikiLine
    {
        public string Name { get; private set; }

        public string Content { get; private set; }

        protected DefineListLine(string line, string name, string content)
            : base(line, LineTypes.DefineList)
        {
            Name = name;
            Content = content;
        }

        /// <summary>
        /// 列表行定义：[至少一个空格] [*-×—] [至少一个空格] 内容
        /// </summary>
        public static readonly Regex Pattern = new Regex(
            @"\A \s+  ([^:]{1,50}) ::\s* (.*)",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.Singleline |
            RegexOptions.IgnorePatternWhitespace);

        /// <summary>
        /// Tries the parse.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <returns></returns>
        [CanBeNull]
        public static DefineListLine TryParse([CanBeNull] string line)
        {
            if (line == null) return null;

            var match = Pattern.Match(line);

            return match.Success
                       ? new DefineListLine(line,
                                      match.Groups[1].Value,
                                      match.Groups[2].Value)
                       : null;
        }

    }
}
